/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "controller.h"
#include "../constants.h"

#include <QtDebug>

Controller::Controller(TTDisp *d, URLFactory *f) {
  ttdisp = d;
  factory = f;
  page = 100;
  subpage = 1;
  pageidbox = NULL;
  bfac = factory->getDefaultInstance();
}

Controller::~Controller() {
  if(bfac != NULL) {
    delete bfac;
  }
}

void Controller::teleTekstChannelChanged(const QString &string) {
  qDebug() << "teleTekstChannelChanged(" << string << ");";
  if(bfac != NULL) {
    delete bfac;
  }
  bfac = factory->createInstance(string);
  page = 100;
  subpage = 1;
  updateTTDisp();
}

void Controller::teleTekstPageChanged(int paginaid) {
  qDebug() << "teleTekstPageChanged(" << paginaid << ");";
  page = paginaid;
  updateTTDisp();
}

void Controller::teleTekstSubPageChanged(int subpaginaid) {
  qDebug() << "teleTekstSubPageChanged(" << subpaginaid << ");";
  subpage = subpaginaid;
  updateTTDisp();
}

void Controller::prevPressed() {
  qDebug() << "prevPressed();";
  if(page > 100) {
    page--;
    if(pageidbox->value() != page) {
      pageidbox->setValue(page);
    }
  }
}

void Controller::nextPressed() {
  qDebug() << "nextPressed();";
  if(page < 999) {
    page++;
    if(pageidbox->value() != page) {
      pageidbox->setValue(page);
    }
  }
}

void Controller::upPressed() {
  qDebug() << "upPressed();";
  if(subpage < TTBQT_MAX_SUB_PAGE) {
    subpage++;
    if(subpageidbox->value() != subpage) {
      subpageidbox->setValue(subpage);
    }
  }
}

void Controller::downPressed() {
  qDebug() << "downPressed();";
  if(subpage > 1) {
    subpage--;
    if(subpageidbox->value() != subpage) {
      subpageidbox->setValue(subpage);
    }
  }
}

void Controller::updateTTDisp() {
  ttdisp->setTeletekstPage(*(bfac->getPageURL(page, subpage)));
  if(pageidbox->hasFocus()) {
    pageidbox->clearFocus();
  }
  if(subpageidbox->hasFocus()) {
    subpageidbox->clearFocus();
  }
}

void Controller::setPageSpinBox(QSpinBox *pidb) {
  pageidbox = pidb;
}

void Controller::setSubPageSpinBox(QSpinBox *spidb) {
  subpageidbox = spidb;
}

void Controller::helpPressed() {
  qDebug() << "helpPressed();";
  ttdisp->displayHelp();
}

void Controller::setPageFocus() {
  pageidbox->setFocus();
  // Select all allows a user to start typing immediatly
  pageidbox->selectAll();
}
